package com.belazy.auto.configure;

import com.belazy.auto.configure.properties.SmsProperties;
import com.belazy.auto.configure.service.ISmsService;
import com.belazy.auto.configure.service.impl.SmsService;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author tangcp
 * @Configuration用来声明该类为一个配置类；
 * @ConditionalOnClass注解说明只有当ISmsService类存在于classpath中时才会进行相应的实例化；
 * @EnableConfigurationProperties将application.properties中对应的属性配置设置于SmsProperties对象中； 自动装配配置类
 */
@Configuration
@ConditionalOnClass(ISmsService.class)
@EnableConfigurationProperties(SmsProperties.class)
public class SmsAutoConfiguration {
    private final SmsProperties msgProperties;
    public SmsAutoConfiguration(SmsProperties msgProperties) {
        this.msgProperties = msgProperties;
    }

    /**
     * @return
     * @ConditionalOnMissingBean指明当容器中不存在ISmsService的对象时再进行实例化；
     * @ConditionalOnProperty指定了配置文件中sms.enabled=true时才进行相应的实例化。
     */
    @Bean
    @ConditionalOnMissingBean(ISmsService.class)
    @ConditionalOnProperty(prefix = "sms", value = "enabled", havingValue = "true")
    public ISmsService iSmsService() {
        return new SmsService (msgProperties);
    }
}
